<template>
  <div class="">
    <div class="">
      <div class="">
        <SearchComp :searchForm="searchForm" :searchData="queryString" :searchAction="searchAction"></SearchComp>
      </div>
      <div class="p-t">
        <div class="btnStyle flex-between ">
          <ButtonComp :buttonData="buttonData" />
        </div>
        <TableCompEle class="m-t" ref="multipleTable" :tableData="tableData" :tableConfig="tableConfig" :tableAction="tableAction" :isSelect="true"
          :isPageIndex="true" :isShowPage="true" :queryParams="queryParams" @getPagination="getList" @handleSelectionChange="handleSelectionChange">
        </TableCompEle>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getPublicDataList,
} from '@/api/yqgx/publish'
import {
  updateFacilities,
} from '@/api/yqgx/facilities'
export default {
  data() {
    return {
      multipleSelection: [],
      queryParams: {
        total: 20,
        currentPage: 1,
        pageSize: 10,
        resourceType: 'facilities'
      },
      buttonData: [
        {
          kind: 'button',
          label: '批量发布',
          plain: false,
          size: 'small',
          type: 'primary',
          click: () => {
            if (this.multipleSelection.length == 0) {
              this.$message({
                type: 'warning',
                message: '请选择要发布的数据'
              })
              return
            }
            this.multipleSelection.forEach(items => {
              this.changeReleaseStatus(items, '发布')
            })
          }
        },
        {
          kind: 'button',
          label: '批量撤销发布',
          plain: false,
          size: 'small',
          type: 'danger',
          click: () => {
            if (this.multipleSelection.length == 0) {
              this.$message({
                type: 'warning',
                message: '请选择要撤销发布的数据'
              })
              return
            }
            this.multipleSelection.forEach(items => {
              this.changeReleaseStatus(items, '未发布')
            })
          }
        }
      ],
      tableData: [
      ],
      tableConfig: [
        {
          prop: 'facilityNum',
          label: '资产编号'
        },
        {
          prop: 'facilityNameCn',
          label: '设施名称',
          isLink: true,
          type: 'primary',
          method: row => {
            this.$router.push({
              name: 'facilitiesInfo',
              params: { flag: 'look', id: row.id }
            })
          }
        },
        {
          prop: 'type',
          label: '设施类别'
        },
        {
          prop: 'managementUnit',
          label: '管理单位'
        },
        {
          prop: 'yearStr',
          label: '启用日期'
        },
        {
          prop: 'investmentScale',
          label: '投资规模(万元)'
        },
        {
          prop: 'sharedScope',
          label: '共享范围'
        }
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(290),
        operations: [
          {
            label: '查看',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$router.push({
                name: 'facilitiesInfo',
                params: { flag: 'look', id: row.id }
              })
            }
          },
          {
            label: '预发布',
            isLink: true,
            type: 'primary',
            isShow: row => {
              return (row.releaseStatus == '未发布')
            },
            method: row => {
              this.changeReleaseStatus(row, '预发布')
            }
          },
          {
            label: '撤销预发布',
            isLink: true,
            type: 'danger',
            isShow: row => {
              return (row.releaseStatus == '预发布')
            },
            method: row => {
              this.changeReleaseStatus(row, '未发布')
            }
          },
          {
            label: '退回预发布',
            isLink: true,
            type: 'danger',
            isShow: row => {
              return (row.releaseStatus == '发布')
            },
            method: row => {
              this.changeReleaseStatus(row, '预发布')
            }
          },
          {
            label: '发布',
            isLink: true,
            type: 'primary',
            isShow: row => {
              return (row.releaseStatus == '预发布' || row.releaseStatus == '未发布')
            },
            method: row => {
              this.changeReleaseStatus(row, '发布')
            }
          },
          {
            label: '撤销发布',
            isLink: true,
            type: 'danger',
            isShow: row => {
              return (row.releaseStatus == '发布')
            },
            method: row => {
              this.changeReleaseStatus(row, '未发布')
            }
          },
          {
            label: '置顶',
            isLink: true,
            type: 'primary',
            isShow: () => {
              return (this.hasTopping != true)
            },
            method: row => {
              this.changeToppingStatus(row, '1')
            }
          },
          {
            label: '撤销置顶',
            isLink: true,
            type: 'danger',
            isShow: row => {
              return ((this.hasTopping == true) && (row.Topping == '1'))
            },
            method: row => {
              this.changeToppingStatus(row, '0')
            }
          },
        ]
      },
      queryString: {
        name: '',
      },
      searchForm: {
        changeMethod: () => {
          this.queryParams.currentPage = 1
        },
        formData: [
          {
            type: 'Input',
            label: '科研设施名称',
            prop: 'name',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
          {
            type: 'Input',
            label: '科研设施类型',
            prop: 'name',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请选择'
          },
          {
            type: 'Input',
            label: '管理单位',
            prop: 'name',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          }
        ]
      },
      searchAction: [
        {
          label: '查询',
          type: 'primary',
          handle: () => {
            this.queryParams.currentPage = 1
            this.$refs.multipleTable.clearSelection()
          }
        },
        {
          label: '重置',
          type: 'primary',
          plain: true,
          handle: () => {
            this.queryString = {
              name: '',
              yearStr: ''
            }
            this.queryParams.currentPage = 1
          }
        }
      ],
      hasTopping: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick() { },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getList() {
      this.queryParams.report = 'report'
      getPublicDataList(this.queryParams).then((res) => {
        this.tableData = res.data
        this.queryParams.total = res.total
        if (res.data.filter(item => item.Topping == '1').length != 0) {
          this.hasTopping = true
        } else {
          this.hasTopping = false
        }
      })
    },
    changeReleaseStatus(row, status) {
      let that = this
      row.releaseStatus = status
      updateFacilities(row).then(() => {
        that.getList()
      })
    },
    changeToppingStatus(row, status) {
      let that = this
      row.Topping = status
      updateFacilities(row).then(() => {
        that.getList()
      })
    }
  }
}
</script>

<style lang="less" scoped></style>